package org.devops

// scan
def SonarScan(sonarServer, projectName, projectDesc, projectPath, runOpts='noset', projectId='', commitSha='', branchName='') {
    def servers = ["test":"sonarqube-test", "prod":"sonarqube-prod"]

    withSonarQubeEnv("${servers[sonarServer]}") { // If you have configured more than one global server connection, you can specify its name
        def scannerHome = "/usr/local/sonar-scanner-4.2.0.1873-linux/"
        //def sonarServer = "http://192.168.57.15:9000"
        def sonarDate = sh returnStdout: true, script: 'date +%Y%m%d%H%M%S'
        sonarDate = sonarDate - "\n"

        println(runOpts)
        if (runOpts == "GitlabPush") {
            sh """
                ${scannerHome}/bin/sonar-scanner \
                -Dsonar.projectKey=${projectName} \
                -Dsonar.projectName=${projectName} \
                -Dsonar.projectVersion=${sonarDate} \
                -Dsonar.ws.timeout=30 \
                -Dsonar.projectDescription=${projectDesc} \
                -Dsonar.links.homepage=https://www.baidu.com \
                -Dsonar.sources=${projectPath} \
                -Dsonar.sourceEncoding=UTF-8 \
                -Dsonar.java.binaries=target/classes \
                -Dsonar.java.test.binaries=target/test-classes \
                -Dsonar.java.surefire.report=target/surefire-reports \
                -Dsonar.gitlab.project_id=${projectId} \
                -Dsonar.gitlab.commit_sha=${commitSha} \
                -Dsonar.gitlab.ref_name=${branchName} \
                -Dsonar.branch.name=${branchName}
            """
        } else {
            sh """
                ${scannerHome}/bin/sonar-scanner \
                -Dsonar.projectKey=${projectName} \
                -Dsonar.projectName=${projectName} \
                -Dsonar.projectVersion=${sonarDate} \
                -Dsonar.ws.timeout=30 \
                -Dsonar.projectDescription=${projectDesc} \
                -Dsonar.links.homepage=https://www.baidu.com \
                -Dsonar.sources=${projectPath} \
                -Dsonar.sourceEncoding=UTF-8 \
                -Dsonar.java.binaries=target/classes \
                -Dsonar.java.test.binaries=target/test-classes \
                -Dsonar.java.surefire.report=target/surefire-reports \
                -Dsonar.branch.name=${branchName}
            """
        }
    }
}